import { ElButton, ElSpace } from 'element-plus'
import { defineComponent } from 'vue'
import { VxeGrid } from 'vxe-table'

import type { IAllocatedPageItem } from '@/apis'
import { StatusTag } from '@/components'

import { AddUserDialog } from './components'
import { useGrid } from './hooks'

export default defineComponent({
  setup() {
    const { gridExtHook, selectedIds, ...gridRestHook } = useGrid()

    return {
      gridRef: gridExtHook.gridRef,
      selectedIds,
      ...gridRestHook
    }
  },
  render() {
    return (
      <div>
        <VxeGrid ref={(e: any) => (this.gridRef = e)} {...this.gridConfig}>
          {{
            toolbar_buttons: () => (
              <ElSpace>
                <ElButton type="primary" plain onClick={this.handleShowAdd}>
                  添加用户
                </ElButton>
                <ElButton
                  type="danger"
                  plain
                  disabled={this.selectedIds.size === 0}
                  onClick={this.handleBatchCancelAuth}
                >
                  批量取消授权
                </ElButton>
                <ElButton type="primary" plain onClick={this.handleClose}>
                  关闭
                </ElButton>
              </ElSpace>
            ),
            columns_status: ({ row }: { row: IAllocatedPageItem }) => (
              <StatusTag value={row.status} />
            ),
            columns_operation: ({ row }: { row: IAllocatedPageItem }) => (
              <ElSpace>
                <ElButton
                  type="primary"
                  size="small"
                  link
                  onClick={() => this.handleCancelAuth(row.id)}
                >
                  取消授权
                </ElButton>
              </ElSpace>
            )
          }}
        </VxeGrid>
        {/* 添加用户 */}
        <AddUserDialog
          ref={(e: any) => (this.addUserDialogRef = e)}
          onSuccess={this.handleSuccessAdd}
        />
      </div>
    )
  }
})
